// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */

#include "CDAMP1Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CDAMP1Data.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CDAMP1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CDAMP1/CDAMP1.md>
*/
// 以下需要修改

enum CDAMP1Index : std::uint8_t
{
    CDAMP1Index_EID = 2,
    CDAMP1Index_PID = 3,  ///< property Id
    CDAMP1Index_G1  = 4,  ///< 节点id
    CDAMP1Index_C1  = 5,
    CDAMP1Index_G2  = 6,
    CDAMP1Index_C2  = 7,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCDAMP1 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时的CDAMP1数据对象
    SG::DataStructure::FEM::CDAMP1Data tempCDAMP1;

    // TODO(bug 001) 正确填写m_dominId，当前默认赋值为1
    tempCDAMP1.m_domainId = 1;

    // 初始化当前词条的索引为1
    int curIDKey = 1;

    // 遍历解析后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 跳过空白词条
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case CDAMP1Index_EID:
            tempCDAMP1.m_id = convertToI64 (curStrKey);
            // 参阅MSC_Nastran_2023.3_Quick_Reference_Guide的1344页  Property identification number of a PDAMP property entry. (Integer >
            // 0;Default = EID)
            tempCDAMP1.m_pId = tempCDAMP1.m_id;
            break;
        case CDAMP1Index_PID:
            tempCDAMP1.m_pId = convertToI64 (curStrKey);
            break;
        case CDAMP1Index_G1:
            tempCDAMP1.m_g[0] = convertToI64 (curStrKey);
            break;
        case CDAMP1Index_C1:
            tempCDAMP1.m_C1 = convertToI64 (curStrKey);
            break;
        case CDAMP1Index_G2:
            tempCDAMP1.m_g[1] = convertToI64 (curStrKey);
            break;
        case CDAMP1Index_C2:
            tempCDAMP1.m_C2 = convertToI64 (curStrKey);
            break;
        default:
            break;
        }
        // 增加当前词条的索引
        curIDKey++;
    }

    // 将解析后的CDAMP1数据添加到数据库
    return dbServiceFactory->get<IElementService> ()->Add (tempCDAMP1);
}
